<?php
namespace app\wechat\controller;

use think\Controller;
use think\View;

use WxpayAPI\WxPayApi;
use WxpayAPI\WxPayNotify;
use WxpayAPI\JsApiPay;
use WxpayAPI\WxPayUnifiedOrder;

use think\Db;
use Des3\DES3;

class Pay extends Common
{
    // 微信支付
    public function wx_pay()
    {
        $type = input('param.type');
        $order = [];
        $attach['type'] = $type;
        $member_type = input('param.member_type');

        // 微信支付签名验证
        $des = new DES3();
        $attach['pay_check_sing'] = $des->encrypt('Mobom');


        /*获取优惠券数据*/
        $member_coupon_id = input("param.member_coupon_id", 0);
        $coupon = [];
        // 优惠券信息
        if ($member_coupon_id > 0) {
            $coupon = db('member_coupon')->alias('mc')->field('c.*')
                ->join('mob_coupon c', 'c.id = mc.coupon_id')
                ->where('mc.member_id', session('member_id'))
                ->where('mc.status', 0)
                ->where('c.end_use_time', '> time', time())
                ->where('c.start_use_time', '<= time', time())
                ->where('mc.id', $member_coupon_id)->find();
        }
        switch ($type) {
            case 'order':
                $order_id = input("param.order_id");
                $model = input("param.model");
                if($model != 'order_buy' && $model != 'order_server') return ['data'=>'','code'=>400061,'msg'=> 'FAULT']; // 订单模型错误

                // 订单信息
                $order = db($model)->field('order_sn, order_price')
                    ->where('id', $order_id)
                    ->where('member_id', session('member_id'))
                    ->find();
                $attach['c'] = 0; // 优惠券
                $attach['cp'] = 0;  // 优惠券金额
                if ($coupon) {
                    $order['order_price'] -= $coupon['money'];
                    $attach['c'] = $member_coupon_id;
                    $attach['cp'] = $coupon['money'];
                }
                $order['order_title'] = '订单支付';

                //  $attach 长度限制  改写model参数和值   order_buy = 1    order_server = 2
                $attach['m'] = $model == 'order_buy' ? 1 : 2;

                break;
            case 'deal':
                $order_id = input("param.order_id");
                $model = input("param.model");
                if( $model != 'order_server_deal' ) return ['data'=>'','code'=>400061,'msg'=> 'FAULT']; // 订单模型错误

                // 订单信息
                $order = db($model)->field('order_sn, order_price')
                    ->where('id', $order_id)
                    ->where('member_id_to', session('member_id'))
                    ->find();
                $attach['c'] = 0;
                $attach['cp'] = 0;
                if ($coupon) {
                    $order['order_price'] -= $coupon['money'];
                    $attach['c'] = $member_coupon_id;
                    $attach['cp'] = $coupon['money'];
                }
                $order['order_title'] = '订单支付';
                // $attach 长度限制  改写model参数和值   采购订单 = 3
                // $attach['m'] = 3;
                break;
            case 'recharge':
                $order_id = input("param.order_id");
                $data = db('order_recharge')->where('id', $order_id)->find();
                $order['order_title'] = '充值';
                $order['order_id'] = $order_id;
                $order['order_price'] = $data['order_price'];
                $order['order_sn'] = $data['order_sn'];
                $attach['member_type'] = $member_type;
                break;
            case 'scan_pay':
                $order_id = input("param.order_id");

                $data = db('order_scan_code_pay')->where('id', $order_id)->find();
                $attach['c'] = 0;
                $attach['cp'] = 0;
                if ($coupon) {
                    $order['order_price'] -= $coupon['money'];
                    $attach['c'] = $member_coupon_id;
                    $attach['cp'] = $coupon['money'];
                }
                //$attach['model'] = 'scan_pay';

                $order['order_title'] = '扫码付款';
                $order['order_id'] = $order_id;
                $order['order_price'] = $data['order_price'];
                $order['order_sn'] = $data['order_sn'];
                break;
            default:
                return json(array('state' => 400, 'msg' => 'zhifu！！'));
                break;
        }

        //return json(array('state' => 400, 'msg' => '系统繁忙！！'));
        if($order) {
            $param = array('order_sn'=>$order['order_sn'], 'order_title'=>$order['order_title'], 'method'=>'1', 'order_price'=>$order['order_price'], 'openid'=>session('openid'));
            $view = new View();

            // 通过接口获取微信支付信息
            $param['attach'] = json_encode($attach);
            $wx_data = call_interface('wx_web_pay', $param);

            if ($wx_data['code'] != 200) {
                return json($wx_data);
            }
            $view->assign('order_id', $order_id);
            $view->assign('member_type', $member_type);
            $view->assign('type', $type);
            $view->assign('order_price', $order['order_price']);
            $view->assign('jsApiParameters', $wx_data['jsApiParameters']);
            $wxpay_html =  $view->fetch();
            exit($wxpay_html);
        } else {
            return json(array('state' => 400, 'msg' => '系统繁忙！！'));
        }
    }

    // 支付页面
    public function order_pay()
    {
        $member_type = input('param.type', 0);
        $model = input('param.model', '');
        if ($model == 'order_recharge') { // 充值
            $money = input('param.m');
            
            // 生成充值订单
            $param = [
                'member_id' => session('member_id'),
                'type' => $member_type,
                'money' => $money,
                'source' => 'web',
            ];
            $data = call_interface('creation_recharge_order', $param);

            // 补充订单数据  前台使用
            $data['data']['type'] = 'recharge';
            $data['data']['model'] = 'recharge';

        } elseif ($model == 'scan_pay') { // 扫码支付
            $money = input('param.m');
            $obj_id = input('param.obj_id', 0);
            $obj_type = input('param.obj_type', 0);
            // 生成扫码支付订单
            $param = [
                'member_id' => session('member_id'),
                'member_type' => 0, // 微信端无法区分付款角色   默认使用普通用户
                'obj_id' => $obj_id,
                'obj_type' => $obj_type,
                'money' => $money,
                'source' => 'web',
            ];
            $data = call_interface('creation_scan_code_pay_order', $param);

            // 补充订单数据  前台使用
            $data['data']['type'] = 'scan_pay';
            $data['data']['model'] = 'scan_pay';

        } elseif ($model == 'order_server_deal') { // 销售采购
            $id = input('param.id', 0);
            $param['member_id'] = session('member_id');
            $param['model'] = input('param.model');
            $param['order_id'] = $id;
            $data = call_interface('deal_pay', $param);
            $data['data']['type'] = 'deal';
        } else {
            $id = input('param.id', 0);
            if (!$id || $model != 'order_server' && $model != 'order_buy') {
                return view('public/error');
            }
            $param['member_id'] = session('member_id');
            $param['model'] = input('param.model');
            $param['order_id'] = $id;
            $data = call_interface('order_pay', $param);
            $data['data']['type'] = 'order';
        }
        if($data['code'] == 200) {
            $this->assign('model',  $model);
            $this->assign('data', $data['data']);
            return view();
        } else {
            //print_r($data);exit();
            $this->assign('code', $data['code']);
            return view('public/error');
        }

    }

    // 支付成功
    public function pay_ok() {
        $type = input('param.type');
        $this->assign('order_id', input('param.order_id'));
        $this->assign('order_price', input('param.order_price'));
        $controllers = ['member','r_member','d_member','p_member'];
        $this->assign('controller', $controllers[input('param.member_type')]);
        switch ($type) {
            case 'order':
                return view();
                break;
            case 'deal':
                return view('deal_ok');
                break;
            case 'recharge':
                return view('recharge_ok');
                break;
            case 'scan_pay':
                return view('scan_pay_ok');
                break;
        }
        return view('public/error');
    }

    // 余额支付
    public function balance_paid() {
        $param = [
            'member_id' => session('member_id'),
            'order_id' => input('param.order_id'),
            'model' => input('param.model'),
            'coupon_id' => input('param.member_coupon_id'),
        ];
        $data = call_interface('balance_paid', $param);
        if ($data['code'] == 200) {
            $data['url'] = url("pay_ok", ["order_id" => input('param.order_id'), "order_price" => $data['data']['order_price'],"member_type" => input('param.member_type'), "type" => input('param.type')]);
        }
        return json($data);
    }
}
